<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 积分公共数据获取
 * 
 */
namespace osc\common\service;
use think\Db;
class Points{
	
	
	//产品详情
	public function get_info($goods_id){
		
		if(!$goods=Db::name('goods')->find($goods_id)){
			return false;
		}
		//使用图片详情
		$image=Db::name('goods_mobile_description_image')->where('goods_id',$goods['goods_id'])->order('sort_order asc')->select();
		return array(
			'goods'=>$goods,
			'image'=>$image,
			'options'=>osc_goods()->get_goods_options($goods['goods_id'])
		);		
	}
	
	//下订单
	public function add_order($param){
		$cart=Db::name('cart')->where('cart_id',$param['cart_id'])->find();
		$goods_option=osc_cart()->get_cart_options($cart['goods_id'],json_decode($cart['goods_option']));
		//查询商品
		$product=Db::name('goods')->where('goods_id',$cart['goods_id'])->find();
		//查询地址
		$address=Db::name('address')->find($param['shipping_address_id']);
		//查询规格
		$goods_attribute = Db::name('goods_attribute')->where(['goods_id'=>$cart['goods_id'],'attribute_value_id'=>$cart['attribute_value_id']])->find();

		//订单z数据
		$order['uid']=$param['uid'];	
		$order['agent_id']=$param['agent_id'];
		$order['cart_id'] = $param['cart_id'];
		$order['type']=config('points_goods');		
		$order['order_num_alias']=build_order_no();
		
		$order['name']=$param['username'];		
		$order['email']=$param['email'];
		$order['tel']=$param['username'];		
		$order['comment']=$param['comment'];

		$order['shipping_name']=$address['name'];				
		$order['shipping_city_id']=$address['city_id'];		
		$order['shipping_country_id']=$address['country_id'];
		$order['shipping_province_id']=$address['province_id'];
		$order['shipping_tel']=$address['telephone'];	
		$order['shipping_method']=config('default_transport_id');
		$order['address_id']=$param['shipping_address_id'];		
		$order['order_status_id']=config('paid_order_status_id');;		
		$order['ip']=get_client_ip();		
		$order['date_added'] =time();
		if($cart['attribute_value_id'] != 0){
			$order['total'] =$goods_attribute['money'];
		}else{
			$order['total'] =$product['price'];
		}
		$order['user_agent']=$_SERVER['HTTP_USER_AGENT'];
		$order['payment_code']='points';
		$order['pay_subject']=$product['name'];
		$order['free_shipping']=1;

		$order_id=Db::name('second_order')->insert($order,false,true);


		//商品
		$goods['order_id']=$order_id;
		$goods['order_num_alias']=$order['order_num_alias'];
		$goods['type']=3;
		$goods['goods_id']=$product['goods_id'];
		$goods['name']=$product['name'];
		$goods['image']=$product['image'];
		$goods['quantity']=1;
		if($cart['attribute_value_id'] != 0){
			$goods['price']=$goods_attribute['money'];
			$goods['total']=$goods_attribute['money'];
		}else{
			$goods['price']=$product['price'];
			$goods['total']=$product['price'];
		}

		$goods['uid']=$order['uid'];
		$goods['order_status_id']=$order['order_status_id'];

		$order_goods_id=Db::name('second_goods')->insert($goods,false,true);

		//商品选项
		if(!empty($goods_option)){		
			foreach ($goods_option as $k => $v) {
				Db::name('order_option')->insert(array(
				'order_id'=>$order_id,
				'goods_id'=>$product['goods_id'],
				'order_goods_id'=>$order_goods_id,
				'option_id'=>$v['option_id'],
				'option_value_id'=>$v['option_value_id'],				
				'name'=>$v['name'],
				'value'=>$v['value'],
				'type'=>$v['type']				
				),false,true);	
			}
		}
		$totals[0]=array(
			'code'=>'sub_total',
			'title'=>'商品价格',
			'text'=>'￥'.$product['pay_points'],
			'value'=>$product['pay_points']				
			);
		$totals[1]=array(
			'code'=>'shipping',
			'title'=>'运费',
			'text'=>'￥ 0',
			'value'=>0				
		);				
		$totals[2]=array(
			'code'=>'total',
			'title'=>'总价',
			'text'=>'￥'.$product['pay_points'],
			'value'=>$product['pay_points']			
		);

		foreach ($totals as $k => $v) {
			Db::name('order_total')->insert(array(
			'order_id'=>$order_id,
			'code'=>$v['code'],
			'title'=>$v['title'],
			'text'=>$v['text'],
			'value'=>$v['value']							
			),false,true);	
		}

		//积分
		$member = Db::name('member')->where('uid',$order['uid'])->find();
		Db::name('predeposit')->insert(array(
				'uid'=>$order['uid'],
				'username'=>session("username"),
				'order_id'=>$order_id,
				'order_num_alias'=>$order['order_num_alias'],
				'money'=>$product['pay_points'],
				'description'=>'购买银豆商品,购买前银豆为：'.$member['points'].'。购买后银豆为：'.($member['points']-$order['total']),
				'prefix'=>2,
				'creat_time'=>time(),
				'pay_time'=>time(),
				'pay_status'=>1
			));

		//剩余
		//Db::name('member')->where('uid',$order['uid'])->setInc('cash_points',$product['pay_points']);	//已经兑换
		
		//写入积分记录
	    /*
		Db::name('points')->insert([
			'uid'=>$order['uid'],
			'order_id'=>$order_id,
			'order_num_alias'=>$order['order_num_alias'],
			'points'=>$product['pay_points'],
			'description'=>'积分兑换',
			'prefix'=>2,
			'creat_time'=>time(),
			'type'=>1
		]);
		*/
		
		//删除购物车中所有积分类商品记录
//		Db::name('cart')->where(array('uid'=>$param['uid'],'type'=>$order['type']))->delete();

		osc_order()->update_second_order($order_id,$param['cart_id']);

		return array('url'=>url('/pay_success'));
	}
	
	//验证订单数据
	public function validate_order_data($data){
		
		//用户id
		$uid=$data['uid'];
		//用户选择的收货地址
		$address_id=$data['shipping_address_id'];
		//用户剩余的积分
		$user_points=$data['user_points'];
		$cart=osc_cart();
		
		if(!$uid){
			return array('error'=>'请先登录');
		}
		
		if(!Db::name('cart')->where('type',3)->find()){
			return array('error'=>'没有选择要下单的商品');
		}
		
		if($address_id==''){
			return array('error'=>'请选择收货地址');
		}
		
		// 验证商品数量		
		$cart_list=Db::name('cart')->find($data['cart_id']);
			
		$param['option']=json_decode($cart_list['goods_option'], true);
		$param['goods_id']=$cart_list['goods_id'];
		$param['quantity']=$cart_list['quantity'];
		
		//判断商品是否存在，验证最小起订量   		
   		if($error=$cart->check_minimum($param)){   			
   			return $error;			
   		}			
				
		//验证商品数量和必选项
		if($error=$cart->check_quantity($param)){			
			return $error;
		}		
		
		//兑换所需积分
		$goods=Db::name('goods')->find($cart_list['goods_id']);
		
		if($user_points<$goods['pay_points']){
			return array('error'=>'积分不足，剩余积分'.$user_points);
		}
		
		
	}
	
}  